<template>
  <el-container id="managerHome">
    <el-header class="Header">
      <div class="logo">
        <img id="logo" src="@/assets/Logo.jpeg" alt="" />
        <h3 style="color: white">志愿填报后台管理系统</h3>
      </div>
      <div class="exit">
        <h3>{{ showName }}</h3>
        <el-button type="danger" plain @click="exit()">返回首页</el-button>
        <el-button type="danger" plain @click="logout()">退出登录</el-button>
      </div>
    </el-header>
    <el-container class="content">
      <!-- 左侧菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollpase">|||</div>
        <el-menu
          background-color="rgba(0, 21, 41)"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :router="true"
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.description }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="(subItem, subIndex) in item.children"
              :key="subItem.id"
              @click="saveActivePath('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.description }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="rightContent">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from "axios";
export default {
  name: "info",
  data() {
    return {
      isCollapse: false, // 是否折叠属性
      // 是否折叠
      activePath: "",
      // 左侧菜单
      menuList: [],

      // 样式显隐
      iconObj: {
        // 一级菜单的icon图标
        0: "el-icon-school",
        "-1": "el-icon-collection",
        "-2": "el-icon-s-custom",
        "-3": "el-icon-thumb",
        "-4": "el-icon-postcard",
        "-5": "el-icon-collection-tag",
      },
    };
  },
  methods: {
    // 点击按钮,切换菜单的折叠和展开
    toggleCollpase() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存连接的激活地址
    saveActivePath(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    // 返回首页
    exit() {
      this.$router.replace("/");
    },
    // 登出
    logout() {
      // 清空token
      var _this = this;
      this.$axios.get("/logout").then((res) => {
        console.log(res);
        window.sessionStorage.removeItem("tokenvalue");
        window.sessionStorage.removeItem("identify");
        _this.$store.commit("logout");
        _this.$router.replace("/login");
      });
    },
    // 获取所有的菜单
    getMenuList() {
      this.$axios
        .get("/back/permission/Menus", {
          headers: {
            satoken: window.sessionStorage.getItem("tokenvalue"),
          },
        })
        .then((res) => {
          console.log(res);
          if (res.data.code === 999) {
            this.$message.error(res.msg);
          } else if (res.data.code === 200) {
            this.menuList = res.data.data;
          }
        });
    },
  },
  // 获得用户名
  computed: {
    showName() {
      return JSON.parse(window.sessionStorage.getItem("managername" || "[]"));
    },
  },
  // 初始化 获得菜单
  created() {
    this.getMenuList();
  },
};
</script>

<style scoped>
* {
  padding: 0;
  margin: 0;
}

.el-container:first-child {
  height: 657px;
  background-color: rgba(0, 21, 41);
}

#managerHome {
  display: flex;
  flex-direction: column;
  font-size: 16px;
}

.Header {
  z-index: 999;
  position: relative;
  width: 100%;
  height: 60px;
  background-color: rgba(0, 21, 41);
}

.hashFixed {
  position: fixed;
  top: 0;
  z-index: 999;
}

.Header-active {
  position: fixed;
  top: 0;
}

.logo {
  float: left;
  width: 250px;
  height: 100%;
  margin-left: 50px;
}

#logo {
  float: left;
  width: 35px;
  height: 35px;
  margin-top: 12.5px;
}

.logo h3 {
  margin-left: 15px;
  float: left;
  height: 60px;
  line-height: 60px;
  text-align: center;
}

.exit {
  position: absolute;
  right: 20px;
  top: 15px;
  width: 300px;
  text-align: center;
}

.exit h3 {
  display: inline-block;
  margin-right: 5px;
  color: white;
}

.exit .el-button {
  width: 80px;
  height: 30px;
  line-height: 30px;
  text-align: center;
}

.el-aside {
  background-color: rgba(0, 21, 41);
  height: 100%;
}

.toggle-button {
  background: rgba(240, 242, 245);
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  color: #333;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-aside .el-menu {
  margin-top: 30px;
  border: none;
}

.el-menu-item {
  margin-bottom: 10px;
  width: 100%;
  text-align: center;
}

.el-menu span {
  font-size: 16px;
}

.el-main {
  background-color: #eaedf1;
}

.rightContent {
  background-color: rgba(240, 242, 245);
}
</style>